#!/usr/bin/python

##
## Copyright (C) 2010 Luciano Xumerle <luciano.xumerle@gmail.com>
##
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program.  If not, see <http://www.gnu.org/licenses/>.
##

from expression import *
from knowngene import *
import subprocess

import time
import sys

try:
    import pysqlite2.dbapi2 as sqlite
except:
    import sqlite3 as sqlite

###########################################################################

def main():
   """main routine"""

   # pars
   database=''
   datafile=''

   errorMsg="SYNTAX: " + sys.argv[0] + " <DataBase> <datafile.txt>\n"

   if len(sys.argv) != 3:
       print >>sys.stderr, errorMsg
       return

   database=sys.argv[1]
   datafile=sys.argv[2]

   print >>sys.stderr, time.ctime() + " CACHE ANNOTATION DATA"

   kg=knowngene(database)
   kg.createCache()

   print >>sys.stderr, time.ctime() + " START MAP"

   for line in open( datafile ):
       process=line.strip().split("\t")
       # gene name must be located to the first column
       row=[ process[0] ]
       for tt in kg.getAnnotationsCached( process[0] ):
           if tt not in row:
               row.append( tt )
       process.append( ",".join(row) )
       print "\t".join( process )

   print >>sys.stderr, time.ctime() + " END MAP"

if __name__=='__main__': main()

